﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.Auth;
using Zhp.IService.Sys;
using Zhp.Model.Common;
using Zhp.Model.Sys;

namespace Zhp.Api.Controllers.Sys
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Sys)]
    public class SysUserRoleController : ControllerBase
    {
        protected readonly ISysUserRoleService SysUserRole;

        public SysUserRoleController(ISysUserRoleService SysUserRole)
        {
            this.SysUserRole = SysUserRole;
        }

        [SeedButton("分配人员", "角色列表", "角色管理", ApiGroupEnum.Sys)]
        [HttpPost]
        public async Task<PageRet<UserRoleModel>> Index(UserRoleQuery query)
        {
            var (list, userIdList) = await SysUserRole.GetUserRole(query);
            
            return new PageRet<UserRoleModel>
            {
                DataList = list,
                TotalCount = query.TotalCount,
                DataExt = userIdList,
            };
        }

        [SeedButton("分配人员:保存", "角色列表", "角色管理", ApiGroupEnum.Sys)]
        [HttpPost]
        public async Task<bool> Create(UserRoleCreate create)
        {
            return await SysUserRole.Create(create);
        }

        [SeedButton("分配角色", "用户列表", "用户管理", ApiGroupEnum.Sys)]
        [HttpPost]
        public async Task<PageRet<UserRoleByUserModel>> GetRoleUserByUser(UserIdQuery query)
        {
            var (list, userIdList) = await SysUserRole.GetRoleUserByUser(query);

            return new PageRet<UserRoleByUserModel>
            {
                DataList = list,
                TotalCount = query.TotalCount,
                DataExt = userIdList,
            };
        }

        [SeedButton("分配角色:保存", "用户列表", "用户管理", ApiGroupEnum.Sys)]
        [HttpPost]
        public async Task<bool> CreateByUser(UserRoleByUserCreate create)
        {
            return await SysUserRole.CreateByUser(create);
        }
    }
}
